<template>
  <div class="chart-component">
    <div :id="'chart-' + component.id" class="chart"></div>
  </div>
</template>

<script setup>
import { onMounted, onUnmounted, watch } from 'vue'
import * as echarts from 'echarts'

const props = defineProps({
  component: {
    type: Object,
    required: true
  }
})

let chartInstance = null

// 生成图表配置
const generateChartOption = (component) => {
  const baseOption = {
    title: {
      text: component.title || '',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      top: 40,
      right: 20,
      bottom: 20,
      left: 40,
      containLabel: true
    },
    color: [component.themeColor || '#3b82f6']
  }

  // 根据图表类型生成不同配置
  switch (component.chartType) {
    case 'line':
      return {
        ...baseOption,
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          type: 'line',
          data: [150, 230, 224, 218, 135, 147, 260],
          smooth: component.smooth,
          label: {
            show: component.showLabel,
            position: 'top'
          }
        }]
      }
    case 'bar':
      return {
        ...baseOption,
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          type: 'bar',
          data: [150, 230, 224, 218, 135, 147, 260],
          label: {
            show: component.showLabel,
            position: 'top'
          }
        }]
      }
    case 'pie':
      return {
        ...baseOption,
        series: [{
          type: 'pie',
          radius: '50%',
          data: [
            { value: 1048, name: 'Search Engine' },
            { value: 735, name: 'Direct' },
            { value: 580, name: 'Email' },
            { value: 484, name: 'Union Ads' },
            { value: 300, name: 'Video Ads' }
          ],
          label: {
            show: component.showLabel
          }
        }]
      }
    // 可以添加更多图表类型...
    default:
      return baseOption
  }
}

onMounted(() => {
  initChart()
})

onUnmounted(() => {
  if (chartInstance) {
    chartInstance.dispose()
  }
})

const initChart = () => {
  const chartDom = document.getElementById(`chart-${props.component.id}`)
  if (!chartDom) return
  
  chartInstance = echarts.init(chartDom)
  updateChart()
}

const updateChart = () => {
  if (!chartInstance) return
  chartInstance.setOption(generateChartOption(props.component))
}

// 监听组件属性变化
watch(() => props.component, () => {
  updateChart()
}, { deep: true })

// 监听容器大小变化
const resizeObserver = new ResizeObserver(() => {
  chartInstance?.resize()
})

onMounted(() => {
  const chartDom = document.getElementById(`chart-${props.component.id}`)
  if (chartDom) {
    resizeObserver.observe(chartDom)
  }
})

onUnmounted(() => {
  resizeObserver.disconnect()
})
</script>

<style scoped>
.chart-component {
  width: 100%;
  height: 100%;
  padding: 12px;
}

.chart {
  width: 100%;
  height: 100%;
}
</style> 